package com.game.utils;

import java.util.List;

import org.dom4j.Element;


public class XmlParserKit {
	/**
	 * 解析类型. 节点解析, 工厂样本库解析
	 */
	public static final String CONTEXTPARSE = "context", FACTORYPARSE = "sample", SCRIPTPARSE = "script";
	/**
	 * 空对象数组
	 */
	public static final Object[] EMPTY_OBJ_ARRAYS = {};

	/**
	 * 根据class类型, 将参数类型转换
	 * 
	 * @param typeClass
	 * @param value
	 * @return
	 */
	public static Object convertType(Class<?> typeClass, Object value) {
		if (value == null)
			return null;
		if (typeClass == String.class)
			return value.toString();
		if (typeClass == int.class) {
			int v = Integer.valueOf((String) value);
			return v;
		} else if (typeClass == long.class)
			return new Long((String) value);
		else if (typeClass == boolean.class)
			return new Boolean((String) value);
		else if (typeClass == byte.class)
			return new Byte((byte) Integer.valueOf(value.toString()).intValue());
		else if (typeClass == short.class)
			return new Short((short) Integer.valueOf(value.toString()).intValue());
		else if (typeClass == char.class)
			return new Character(((String) value).charAt(0));
		else if (typeClass == float.class)
			return new Float((String) value);
		else if (typeClass == double.class)
			return new Double((String) value);
		else if (typeClass == int[].class) {
			return TextKit.parseIntArray(value.toString().split(","));
		} else if (typeClass == String[].class) {
			return value.toString().split(",");
		} else if (typeClass == float[].class) {
			return TextKit.parseFloatArray(value.toString().split(","));
		} else if (typeClass == double[].class) {
			return TextKit.parseDoubleArray(value.toString().split(","));
		} else if (typeClass == char[].class) {
			return TextKit.parseCharArray(value.toString().split(","));
		}
		return value;
	}

	/**
	 * 根据类型, 获得基础类型的class
	 * 
	 * @param obj
	 * @return
	 */
	public static Class<?> getPrimitiveClass(Object obj) {
		Class<?> c = obj.getClass();
		if (c == Boolean.class)
			return boolean.class;
		else if (c == Character.class)
			return char.class;
		else if (c == Byte.class)
			return byte.class;
		else if (c == Short.class)
			return short.class;
		else if (c == Integer.class)
			return int.class;
		else if (c == Long.class)
			return long.class;
		else if (c == Float.class)
			return float.class;
		else if (c == Double.class)
			return double.class;
		return c;
	}


	/**
	 * 获取某个节点下的第一个子节点
	 * 
	 * @param element
	 *            节点
	 */
	public static Element getFirstElement(Element element) {
		List<Element> elements = element.elements();
		if (elements == null || elements.size() <= 0)
			return null;
		return elements.get(0);
	}

}
